package com.ai.demo.service.impl;

import com.ai.demo.service.LangChainService;
import dev.langchain4j.data.message.ChatMessage;
import dev.langchain4j.memory.ChatMemory;
import dev.langchain4j.memory.chat.TokenWindowChatMemory;
import dev.langchain4j.model.input.Prompt;
import dev.langchain4j.model.input.PromptTemplate;
import dev.langchain4j.model.openai.OpenAiChatModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * AUTHER: wangyue
 * TIME  : 2025/2/19 : 11:30
 */
@Service
public class LangChainServiceImpl implements LangChainService {


    @Autowired
    private  OpenAiChatModel chatModel;

//    private final YourLangChainClass langChain;
//
//    public LangChainService() {
//        // 初始化 LangChain 对象
//        this.langChain = new YourLangChainClass();
//    }

//    public String processText(String input) {
//        // 使用 LangChain4j 处理文本
//        return langChain.process(input);
//    }

    public String chat(final String question) {
        final  Prompt prompt = buildPrompt(question);
//        ChatMessage chatMessage = ChatMessage;
//        List<ChatMessage> var1 = new ArrayList<>();
//        var1.add()

        // 使用 LangChain4j 处理文本
        String generate = chatModel.generate(question);
        System.out.println(""+generate);
        return generate;
    }


//    public String chatWithMemory(String userId, String message) {
//        ChatMemory chatMemory = new TokenWindowChatMemory(500); // 基于token的上下文窗口
//        Assistant assistant = Assistant.from(chatModel, chatMemory);
//        return assistant.chat(userId, message);
//    }




    public Prompt buildPrompt(final String question) {
        PromptTemplate promptTemplate = PromptTemplate.from("{{question}}");
        Map<String,Object> variables = new HashMap<>();
        variables.put("question",question);
        return promptTemplate.apply(variables);
    }

}
